require 'rexml/document'
require 'erb'
require 'kconv'
require 'rubygems'
require 'rack'
require 'mm2s5_common'


class Mm2s5

  include Mm2s5Common
  
  attr_reader :title, :subtitle, :contents
  
  def initialize(mm_xml, meta={ })
    @meta = { }
    @meta.default = ""
    create_html(mm_xml)
    @meta.merge!(meta)
  end
  
  def author
    @meta[:author]
  end
  
  def template
    t = @meta[:template]
    t.empty? ? "default" : t
  end
  
  def presdate_fmt
    p = @meta[:presdate]
    if p.nil? || p.empty?
      p = ""
    elsif p == "today"
      p = Time.now.strftime('%Y/%m/%d')
    end
    return p
  end

  def presdate
    presdate_fmt.gsub(/\//, '')
  end
  
  def company
    @meta[:company]
  end
      
  def create_html(mm_xml)
    doc = REXML::Document.new(mm_xml)
    node = first_child(doc.root)
    titles = get_text(node).split(/\n/)
    @title = titles[0]
    @subtitle = titles[1]
    @contents = create_content(node)
  end
  
  def create_content(root)
    s = ""
    root.each_element do |node|
      case get_text(node)
      when  "__meta__"
        @meta = get_metadata(node)
      else 
        s << create_slide(node)
      end
    end
    return s
  end

  def create_slide(node)
    headline = get_text(node)
    child = first_child(node)
    return "" if button_cancel_icon?(child)
    list_start, list_end = create_list_tag(child)
    content = create_slide_content(node)
    
    tmpl=<<EOS
<div class='slide'>
  <h1><%= headline %></h1>
  <div class='slidecontent'>
    <%= list_start %>
      <%= content %>
    <%= list_end %>
  </div>
</div>
EOS
    
    return ERB.new(tmpl).result(binding)
  end
  
  def create_list_tag(child)
    list_start = "<ul>"
    list_end = "</ul>"
    if stop_icon?(child)
      list_start = "<ul class='incremental'>\n"
    elsif first_icon?(child)
      list_start = "<ol>\n"
      list_end = "</ol>\n"
    end
    return list_start, list_end
  end
  
  def create_slide_content(node)
    s = ""
    node.each_element do |child|
      if get_text(child) == "__table__"
        s << create_table(child)
      else
        s << create_list(child)
      end
    end
    return s
  end
  
  def create_table(table_node)
    s = ""
    s << "<table>\n"
    table_node.each_element do |tr_node|
      s << create_tr(tr_node)
    end
    s << "</table>\n"
  end
  
  def create_tr(tr_node)
    s = ""
    s << "<tr>\n"
    s << create_td(tr_node)
    s << "</tr>\n"
    return s
  end
  
  def create_td(node)
    s = ""
    txt = get_text(node)
    s << "<td>#{txt}</td>\n"
    child = first_child(node)
    if !(child.nil?)
      s << create_td(child) # recursive call
    end
    return s
  end

  def create_list(node)
    txt = get_text(node)
    return "" if txt.nil? || txt.empty?
    s = ""
    s << "<li>#{txt}</li>\n"
    if node.has_elements?
      s << "<ul>\n"
      node.each_element do |child|
        s << create_list(child)
      end
      s << "</ul>\n"
    end
    return s
  end

  def get_text(node)
    s = node.attributes['TEXT'].to_s
    s = Rack::Utils.escape_html(s)
    if pencil_icon?(first_child(node))
      s = "<img src='#{s}'/>"
    end
    return s
  end
  
  def get_metadata(meta_node)
    meta = { }
    meta_node.each_element do |e|
      atr = get_text(e)
      val = get_text(first_child(e))
      meta.store(atr.to_sym, val)
    end
    return meta
  end

end
